#!/usr/bin/env bash
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
# Function :
# Platform :
# Version  :1.02
# Date     :2023-02-04
# Author   :mugoLH
# Contact  :
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
#  变量列表 List of common variables
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----

#  需要修改成的目标IP和网关
DEST_IP=""
GATEWAY_IP=""
#  需要修改成的目标主机名
HOST_NAME=""
#  模板服务器IP
DEFAULT_Eg_IP=10.1.1.11
Eg_IP=""
#  模板DNS
Domain_IP=""
#  模板网卡配置文件
DEV_DIR=/etc/sysconfig/network-scripts/ifcfg
DEFAULT_NET_DEV=ens192
NET_DEV=""
DEV_FILE=${DEV_DIR}



#  函数列表 Function list
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
info_p(){    printf "\e[1;32m%-20s \e[0m%-24s %-24s %-2s %-2s\e[0m\n" "${1}" "${2}" "${3}" "${4}" "${5}" >&1 ;}
tips_p(){    printf "\e[1;32m%-10s \e[0m%-40s %-24s %-2s %-2s\e[0m\n" "${1}" "${2}" "${3}" "${4}" "${5}" >&1 ;}


help_f(){
    info_p "[ 说明：]"
    info_p "  此脚本实现模板服务器的IP(包括网关，默认是IP段的1号IP为网关)，主机名修改，可选参数如下"
    info_p "    -d | --domain"   "[dest DNS]"         ":目标DNS"
    info_p "    -e | --eip"      "[demo IP]"          ":demo IP地址"
    info_p "    -f | --netfile"  "[network dev name]" ":demo 网卡设备名(不用加ifcfg)"
    info_p "    -g | --gateway"  "[dest gateway ]"    ":目标的网关（默认修改为IP地址段的第一个，例如：192.168.1.1"
    info_p "    -h | --hostname" "[dest host]"        ":目标服务器的主机名"
    info_p "    -i |---dip"      "[dest IP]"          ":目标IP地址"
    info_p "    --h | --help"    "[help ]"            ":输出帮助说明"
}

# 如果参数为空，则退出
if [ $# -eq 0 ];then
    help_f
    exit 0
else
    #  获取主机组关键字
    while [ -n "$1" ]; do
        case $1 in
            -d | --domain)
                Domain_IP="${2}"
                shift 2
                ;;
            -e | --eip)
                Eg_IP="${2}"
                shift 2
                ;;
            -f | --netfile )
                NET_DEV="${2}"
                shift 2
                ;;
            -g | --gateway )
                GATEWAY_IP="${2}"
                shift 2
                ;;
            -h | --hostname )
                HOST_NAME="${2}"
                shift 2
                ;;
            -i | --dip )
                DEST_IP="${2}"
                shift 2
                ;;
            --h | --help )
                help_f
                exit
                ;;
            *)
                break
                ;;
        esac
    done
    #  循环远程执行命令
    [[ X${Eg_IP} == "X" ]]  && Eg_IP=${DEFAULT_Eg_IP}
    if ! ping -c 3 ${Eg_IP} >/dev/null 2>&1 ;then
        echo "[ ERROR ] DEMO服务器地址：${Eg_IP} 不通，请检查！"
        exit 1
    fi

    [[ X${NET_DEV} == "X" ]] && NET_DEV=${DEFAULT_NET_DEV}
    #  检查ip和网关
    if [[ -z ${GATEWAY_IP} ]] && [[ -n ${DEST_IP} ]];then
        GATEWAY_IP=$(echo ${DEST_IP} | awk -F "." '{print$1"."$2"."$3"."1}')
    elif [[ -z ${GATEWAY_IP} ]] && [[ -z ${DEST_IP} ]]; then
        echo "[ ERROR ] 需要 -i [IP地址] 指定修改的预期目标IP"
        echo "查看帮助，请执行 $0 --help"
        exit 1
    elif [[ -n ${GATEWAY_IP} ]] && [[ -z ${DEST_IP} ]]; then
        echo "[ ERROR ] 需要 -i [IP地址] 指定修改的预期目标IP"
        echo "查看帮助，请执行 $0 --help"
        exit 1
    fi

    if [[ -z ${HOST_NAME} ]];then
        echo "[ ERROR ] 需要 -h [主机名] 指定修改的预期目标主机名"
        echo "\n查看帮助，请执行 $0 --help"
        exit
    fi

    #  网卡绝对路径
    DEV_FILE=${DEV_DIR}-${NET_DEV}
    #  修改DNS
    if [[ -n ${Domain_IP} ]];then
        tips_p "[ INFO  ]" "change host:${Eg_IP} DNS to" "${Domain_IP}"
        ssh root@${Eg_IP} "sed -i \"s/^DNS.*=.*/DNS1=${Domain_IP}/g\" ${DEV_FILE}"
    fi
    #  执行远程修改IP
    tips_p "[ INFO  ]" "change host:${Eg_IP} IP to" "${DEST_IP}"
    ssh root@${Eg_IP} "sed -i 's/${Eg_IP}/${DEST_IP}/g' ${DEV_FILE}"
    #  执行远程修改网关
    tips_p "[ INFO  ]" "change host:${Eg_IP} gateway to" "${GATEWAY_IP}"
    ssh root@${Eg_IP} "sed -i \"s/^GATEWAY=.*/GATEWAY=${GATEWAY_IP}/g\" ${DEV_FILE}"
    #  执行修改主机名
    tips_p "[ INFO  ]" "change host:${Eg_IP} hostname to" "${HOST_NAME}"
    ssh root@${Eg_IP} "hostnamectl set-hostname ${HOST_NAME}"
    #  执行修改网卡UUID
    ssh root@${Eg_IP} "sed -i \"s/^UUID=.*/UUID=$(uuidgen)/g\" ${DEV_FILE}"
    #  执行重启服务器
    tips_p "[ INFO  ]" "reboot host:${Eg_IP}" "new IP：${DEST_IP}"
    ssh root@${Eg_IP} "reboot 0"
fi

